package com.dhwl.hsh.driver.ui.activity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.utils.StringUtils;
import com.dhwl.hsh.lib.utils.ToastUtils;
import com.dhwl.hsh.driver.R;
import com.dhwl.hsh.driver.presenter.UserPresenter;
import com.dhwl.hsh.driver.presenter.iview.ILoginView;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 登录页面
 *
 * @author Derek.Kidd
 * @time 2016/4/8 14:32
 */
public class LoginActivity extends BaseActivity implements ILoginView {


    @Bind(R.id.edit_phone)
    EditText editPhone;

    @Bind(R.id.edit_pwd)
    EditText editPwd;

    @Bind(R.id.btn_login)
    Button btnLogin;

    @Bind(R.id.image_login_loading)
    ImageView imageLoading;

    @Bind(R.id.tv_forgot_pwd)
    TextView tvForgotPwd;


    private String phoneNum;

    private String password;


    //完成按钮的动画效果钮的动画效果
    private AnimationDrawable anim;

    private UserPresenter userPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        btnLogin.setBackgroundResource(R.drawable.comm_bg_btn_grey);
        anim = (AnimationDrawable) imageLoading.getBackground();

        editPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                showSubmitBtn();
            }
        });

        editPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                showSubmitBtn();
            }
        });

    }


    @Override
    protected void initData() {
        userPresenter = new UserPresenter();
        userPresenter.attachView(this);
        //FIXME
        editPhone.setText("13951678171");
        editPwd.setText("123456");
    }


    /**
     * 控制提交按钮是否可以使用
     *
     * @see [类、类#方法、类#成员]
     */
    private void showSubmitBtn() {
        phoneNum = editPhone.getText().toString().trim();
        password = editPwd.getText().toString().trim();

        //手机号不为空并且符合规则时才可点击获取验证码按钮
        if (TextUtils.isEmpty(phoneNum) || TextUtils.isEmpty(password) || !StringUtils.isPhone(phoneNum) || password.length() < 6) {
            btnLogin.setBackgroundResource(R.drawable.comm_bg_btn_grey);
            btnLogin.setEnabled(false);
        } else {
            btnLogin.setBackgroundResource(R.drawable.selector_comm_btn);
            btnLogin.setEnabled(true);
        }
    }

    /**
     * 点击登录按钮后， 参数校验、显示登录进度条
     *
     * @return void [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    @OnClick(R.id.btn_login)
    public void onLoginClick(View v) {
        // 获取字段信息并验证
        phoneNum = editPhone.getText().toString().trim();
        password = editPwd.getText().toString().trim();
        // 账号为空，返回
        if (TextUtils.isEmpty(phoneNum)) {
            ToastUtils.show(this, R.string.user_login_validate_phone_is_null, ToastUtils.LENGTH_SHORT);
            return;
        }
        // 账号手机号规则验证
        if (!StringUtils.isPhone(phoneNum)) {
            ToastUtils.show(this, R.string.input_phone_is_error, ToastUtils.LENGTH_SHORT);
            return;
        }

        // 密码
        if (StringUtils.isEmpty(password)) {
            ToastUtils.show(this, R.string.user_login_validate_pwd_is_null, ToastUtils.LENGTH_SHORT);
            return;
        }
        // 密码长度验证
        if (password.length() < 6 || password.length() > 20) {
            ToastUtils.show(this, getString(R.string.user_login_pwd_validate_length,
                    6,
                    20), ToastUtils.LENGTH_SHORT);
            return;
        }
        startLoading();
        //FIXME
        userPresenter.login(phoneNum, StringUtils.getMD5(password));
    }

    private void startLoading() {
        //隐藏等待状态
        editPhone.setEnabled(false);
        editPwd.setEnabled(false);
        tvForgotPwd.setEnabled(false);
        btnLogin.setBackgroundResource(R.drawable.comm_bg_btn_grey);
        imageLoading.setVisibility(View.VISIBLE);
        anim.start();
    }

    private void endLoading() {
        //隐藏等待状态
        editPhone.setEnabled(true);
        editPwd.setEnabled(true);
        tvForgotPwd.setEnabled(true);
        btnLogin.setBackgroundResource(R.drawable.comm_bg_btn);
        imageLoading.setVisibility(View.INVISIBLE);
        anim.stop();
    }

    @OnClick(R.id.tv_forgot_pwd)
    public void onForgotPwdClick(View v) {
        startActivity(new Intent(LoginActivity.this, ForgotPwdActivity.class));
    }


    @Override
    public void onLoginSuccess(Object object) {
        endLoading();
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    @Override
    public void onLoginFailure(HshException e) {
        endLoading();
    }
}

